##
##	mydns.m4
## autoconf macros for the MyDNS package.
##
##	Copyright (C) 2002  Don Moore <bboy@bboy.net>
##
##	$Header: /pub/cvsroot/mydns/m4/mydns.m4,v 1.23 2002/09/20 21:22:12 bboy Exp $
##

##
## Check for IPv6 support
##
AC_DEFUN([AC_CHECK_IPV6],
	[
		AC_MSG_CHECKING([whether IPv6 is supported])
		AC_TRY_COMPILE([#include <netinet/in.h>], [struct in6_addr addr; memset(&addr,0,sizeof(struct in6_addr));],
			[ AC_DEFINE([HAVE_IPV6], 1, [Is IPv6 supported?])
			  AC_MSG_RESULT([yes]) ],
			AC_MSG_RESULT([no]))
	]
)
 
##
## Check for `sa_len' in `struct sockaddr'
##
AC_DEFUN([AC_CHECK_SOCKADDR_SA_LEN],
	[
		AC_MSG_CHECKING([for sa_len in struct sockaddr])
		AC_TRY_COMPILE([
#if HAVE_SYS_TYPES_H
#	include <sys/types.h>
#endif

#if HAVE_SYS_SOCKET_H
#	include <sys/socket.h>
#endif
], [void *p = &((struct sockaddr *)0L)->sa_len;],
		[ AC_DEFINE(HAVE_SOCKADDR_SA_LEN, 1, [Does struct sockaddr contain sa_len?])
		  AC_MSG_RESULT([yes]) ],
		AC_MSG_RESULT([no]))
	]
)


##
## Check for printf() format (make sure %llu works properly)
##
AC_DEFUN([AC_CHECK_PRINTF_LLU],
	[
		AC_MSG_CHECKING([format specifier for a 64-bit unsigned int])
		AC_TRY_RUN([
#ifdef F77_DUMMY_MAIN
#  ifdef __cplusplus
     extern "C"
#  endif
   int F77_DUMMY_MAIN() { return 1; }
#endif

int main() {
	close(1);
	return (printf("%llu", 18446744073709551615ULL) != strlen("18446744073709551615"));
}
],
		[	AC_DEFINE(PRINTF_NEEDS_QUAD, 0, [Does printf() need %qu instead of %llu?])
			AC_MSG_RESULT([%llu works]) ],
		[ AC_DEFINE(PRINTF_NEEDS_QUAD, 1, [Does printf() need %qu instead of %llu?])
		AC_MSG_RESULT([%qu, I guess...])])
	]
)


##
##	Make --enable-debug turn on debugging
##
AC_DEFUN([AC_ENABLE_DEBUG],
	[
		AC_MSG_CHECKING([whether to enable debugging])
		AC_ARG_ENABLE(debug,
			AC_HELP_STRING([--enable-debug], [enable debugging options]),
			[
				if test "$enableval" = yes
				then
					AC_DEFINE(DEBUG, 1, [Compile with support for debugging options?])
					AC_MSG_RESULT([yes])
					if test "$ac_cv_prog_gcc" = "yes"; then
						CFLAGS="-O -g"
					else
						CFLAGS="-g"
					fi
				else
					AC_MSG_RESULT([no])
				fi
			], AC_MSG_RESULT([no]))
	]
)


##
##	Make --enable-profiling turn on profiling
##
AC_DEFUN([AC_ENABLE_PROFILE],
	[
		AC_MSG_CHECKING([whether to compile for profiling])
		AC_ARG_ENABLE(profiling,
			AC_HELP_STRING([--enable-profiling], [compile for profiling]),
			[
				if test "$enableval" = yes
				then
					CFLAGS="-g -pg -a"
					AC_DEFINE(PROFILING, 1, [Compile for profiling?])
					AC_MSG_RESULT([yes])
				else
					AC_MSG_RESULT([no])
				fi
			], AC_MSG_RESULT([no]))
	]
)


##
## Find zlib compression library (@LIBZ@)
##
AC_DEFUN([AC_LIB_Z],
	[
		ac_mydns_lib_dirs="/lib /usr/lib /usr/local/lib"
		AC_ARG_WITH(zlib,
			AC_HELP_STRING([--with-zlib=DIR],
								[look for the zlib compression library in DIR]),
			ac_mydns_lib_dirs="$withval $ac_mydns_lib_dirs")
		ac_mydns_lib_found=no, ac_mydns_lib_ok=no
		for dir in $ac_mydns_lib_dirs
		do
			if test "$ac_mydns_lib_found" != yes
			then
				AC_CHECK_FILE($dir/libz.a, ac_mydns_lib_found=yes, ac_mydns_lib_found=no)
				if test "$ac_mydns_lib_found" = yes
				then
					AC_CHECK_LIB(z, deflate, ac_mydns_lib_ok=yes, ac_mydns_lib_ok=no)
					test "$ac_mydns_lib_ok" = yes && LIBZ="-L$dir -lz"
				fi
			fi
		done
		if test "$ac_mydns_lib_ok" != yes
		then
			AC_MSG_ERROR([zlib compression library not found.  (Download it from http://www.gzip.org/zlib/)])
		fi
		AC_SUBST(LIBZ)
	]
)

##
## Find MySQL client library (@LIBMYSQLCLIENT@)
##
AC_DEFUN(AC_LIB_MYSQLCLIENT,
	[
		ac_mydns_lib_dirs="/lib /usr/lib /usr/lib/mysql /usr/local/lib \
								 /usr/local/lib/mysql /usr/local/mysql/lib"
		AC_ARG_WITH(mysql-lib,
			AC_HELP_STRING([--with-mysql-lib=DIR],
								[look for the MySQL client library in DIR]),
			ac_mydns_lib_dirs="$withval $ac_mydns_lib_dirs")
		ac_mydns_lib_found=no, ac_mydns_lib_ok=no
		ac_mydns_libs="$LDFLAGS"
		for dir in $ac_mydns_lib_dirs
		do
			if test "$ac_mydns_lib_found" != yes
			then
				AC_CHECK_FILE($dir/libmysqlclient.a, ac_mydns_lib_found=yes, ac_mydns_lib_found=no)
				if test "$ac_mydns_lib_found" = yes
				then
					LIBS="$ac_mydns_libs -L$dir -lmysqlclient $LIBZ"
					AC_CHECK_LIB(mysqlclient, mysql_real_connect, ac_mydns_lib_ok=yes, ac_mydns_lib_ok=no)
					LIBS="$ac_mydns_libs"
					test "$ac_mydns_lib_ok" = yes && LIBMYSQLCLIENT="-L$dir -lmysqlclient"
				fi
			fi
		done
		if test "$ac_mydns_lib_ok" != yes
		then
			AC_MSG_ERROR([MySQL client library not found.  (See config.log for more information.)])
		fi
		AC_SUBST(LIBMYSQLCLIENT)
	]
)

##
##	Find location of MySQL header files (@MYSQL_INCLUDE@)
##
AC_DEFUN(AC_HEADER_MYSQL,
	[
		ac_mydns_header_dirs="/usr/include /usr/include/mysql /usr/local/include \
									 /usr/local/include/mysql /usr/local/mysql/include"
		ac_mydns_header_found=no, ac_mydns_header_ok=no
		AC_ARG_WITH(mysql-include,
			AC_HELP_STRING([--with-mysql-include=DIR],
								[look for MySQL include files in DIR]),
			ac_mydns_header_dirs="$withval $ac_mydns_header_dirs")
		for dir in $ac_mydns_header_dirs
		do
			if test "$ac_mydns_header_found" != yes
			then
				AC_CHECK_FILE($dir/mysql.h, ac_mydns_header_found=yes, ac_mydns_header_found=no)
				test "$ac_mydns_header_found" = yes && MYSQL_INCLUDE="-I$dir"
			fi
		done
		if test "$ac_mydns_header_found" != yes
		then
			AC_MSG_ERROR([Could not find mysql.h.  (See config.log for more information.)])
		fi
		AC_SUBST(MYSQL_INCLUDE)
	]
)

##
## Set better CFLAGS for known system types
##
AC_DEFUN(AC_MYDNS_CFLAGS,
	[
		AC_MSG_CHECKING([CFLAGS])
		test "$CFLAGS" = "-g -O2" -o "$CFLAGS" = "-O2" && ac_mydns_default_cflags=yes

		if test "$ac_mydns_default_cflags" = yes
		then
			if test "$ac_cv_prog_gcc" = "yes"; then

				## Additional optimizations for gcc
				CFLAGS="-O3"
				ac_mydns_systype=`$ac_config_guess`
				case "$ac_mydns_systype" in
					i686-pc-linux-gnu)
						CFLAGS="$CFLAGS -march=i386 -mcpu=i686"
						;;

					i586-pc-linux-gnu)
						CFLAGS="$CFLAGS -march=i386 -mcpu=i586"
						;;

					i386-pc-linux-gnu)
						CFLAGS="$CFLAGS -march=i386 -mcpu=i386"
						;;

					*) ;;
				esac

				## Additional warnings/options for gcc
				CFLAGS="$CFLAGS -Wall"
				CFLAGS="$CFLAGS -Wno-unused"
				CFLAGS="$CFLAGS -fomit-frame-pointer"
				CFLAGS="$CFLAGS -finline-functions"
				CFLAGS="$CFLAGS -ffast-math"
				CFLAGS="$CFLAGS -funsigned-char"
			fi
		fi
		AC_MSG_RESULT($CFLAGS)
	]
)

##
##  Set config file location
##
AC_DEFUN(AC_MYDNS_CONF,
	[
		AC_MSG_CHECKING([for default config file location])
		AC_ARG_WITH(confdir,
			AC_HELP_STRING([--with-confdir=DIR],
								[default config file path  (default is `/etc')]),
			ac_mydns_conf="$withval/mydns.conf", ac_mydns_conf="/etc/mydns.conf")
		MYDNS_CONF="$ac_mydns_conf"
		AC_SUBST(MYDNS_CONF)
		AC_DEFINE_UNQUOTED(MYDNS_CONF, ["$MYDNS_CONF"], [Default location of configuration file])
		AC_MSG_RESULT($MYDNS_CONF)
	]
)

##
## Set some extra variables to make changes easier
##
AC_DEFUN(AC_MYDNS_PKGINFO,
	[
		PACKAGE_HOMEPAGE="http://mydns.bboy.net"
		PACKAGE_COPYRIGHT="Copyright (C) 2002 Don Moore"
		PACKAGE_AUTHOR="Don Moore <bboy@bboy.net>"
		PACKAGE_DATE=`date +"%b %Y"`
		PACKAGE_RELEASE_DATE=`date +"%B %d, %Y"`

		AC_SUBST(PACKAGE_DATE)
		AC_DEFINE_UNQUOTED(PACKAGE_DATE, ["$PACKAGE_DATE"], [Package build date])

		AC_SUBST(PACKAGE_RELEASE_DATE)
		AC_DEFINE_UNQUOTED(PACKAGE_RELEASE_DATE, ["$PACKAGE_RELEASE_DATE"], [Exact package build date])

		AC_SUBST(PACKAGE_HOMEPAGE)
		AC_DEFINE_UNQUOTED(PACKAGE_HOMEPAGE, ["$PACKAGE_HOMEPAGE"], [This package's homepage])

		AC_SUBST(PACKAGE_COPYRIGHT)
		AC_DEFINE_UNQUOTED(PACKAGE_COPYRIGHT, ["$PACKAGE_COPYRIGHT"], [Copyright holder])

		COPYRIGHT_HOLDER=$PACKAGE_COPYRIGHT
		AC_SUBST(COPYRIGHT_HOLDER)

		AC_SUBST(PACKAGE_AUTHOR)
		AC_DEFINE_UNQUOTED(PACKAGE_AUTHOR, ["$PACKAGE_AUTHOR"], [This package's primary author])
	]
)

## vi:set ts=3:
